/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wrap.pages;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import java.awt.event.ActionEvent;
import wrap.core.CC;

/**
 *
 * @author Liang
 */
public class DOBHelper {

    static DOBHelper helper = new DOBHelper();

    DOB1 dob1;// = new DOB1();
    DOB2 dob2;// = new DOB2();
    DOB3 dob3;// = new DOB3();

    boolean installed = false;

    JPanel panel = null;

    public void installPanel(JPanel p){
        if(!installed){
            this.panel = p;
            init();
            CC.getValues().put("DOBTYPE", new Integer(2));
        }
        dob1 = new DOB1();
        dob2 = new DOB2();
        dob3 = new DOB3();
        int i = (Integer)(CC.getValues().get("DOBTYPE"));

        this.switchPanel(i);
        installed = true;
    }

    private void init(){

        System.out.println("init");

        JComponent jc = SwingUtilities.getRootPane(panel);
        JComponent c = jc;//(JComponent)panel.getParent();

        int mod = JComponent.WHEN_IN_FOCUSED_WINDOW;
        Object ShowDOB1 = "ShowDOB1";
        Object ShowDOB2 = "ShowDOB2";
        Object ShowDOB3 = "ShowDOB3";
        c.getInputMap(mod).put(KeyStroke.getKeyStroke("1"), ShowDOB1);
        c.getInputMap(mod).put(KeyStroke.getKeyStroke("2"), ShowDOB2);
        c.getInputMap(mod).put(KeyStroke.getKeyStroke("3"), ShowDOB3);


        c.getActionMap().put(ShowDOB1, new AbstractAction(){
            public void actionPerformed(ActionEvent e) {

                System.out.println(1);
                switchPanel(1);
            }
        });
        c.getActionMap().put(ShowDOB2, new AbstractAction(){
            public void actionPerformed(ActionEvent e) {

                System.out.println(2);
                switchPanel(2);
            }
        });
        c.getActionMap().put(ShowDOB3, new AbstractAction(){
            public void actionPerformed(ActionEvent e) {

                System.out.println(3);
                switchPanel(3);
            }
        });


    }

    private void switchPanel(int i){
        System.out.println(i);
        JComponent c = this.panel;
        c.removeAll();
        JComponent d = null;
        switch(i){
            case 2:
                d = dob2;
                CC.getValues().put("DOBTYPE", new Integer(2));
                break;
            case 3:
                d = dob3;
                CC.getValues().put("DOBTYPE", new Integer(3));
                break;
            default:
                d = dob1;
                CC.getValues().put("DOBTYPE", new Integer(1));
                break;
        }
        c.add(d, java.awt.BorderLayout.CENTER);
        c.validate();
        c.repaint();
        d.requestFocus();
    }


}
